<?php
/**
 * Created by PhpStorm.
 * User: u
 * Date: 2018/11/20
 * Time: 17:32
 */

namespace common\sources\write\buyuser;

use Yii;
use common\models\craftsman\BuyUserAccount;
use yii\db\Exception;

class BuyUserAcountAccess implements IBuyUserAcountAccess
{
    public function addUserBank($data) {
        $userAmount = BuyUserAccount::find()
            ->where(['buy_id' => $data['id']])
            ->one();
        if ($userAmount) {
            $userAmount->remail_golds = intval($userAmount->remail_golds*100 + $data['money']*100)/100;
            if ($userAmount->save()) {
                return $userAmount;
            }
        }
        return false;
    }

    //调整佣金/押金
    public function changeUserBank($data) {
        $userAmount = BuyUserAccount::find()
            ->where(['buy_id' => $data['id']])
            ->one();
        if ($userAmount) {
            switch ($data['action_type']) {
                case 1://充值
                    $userAmount->remail_golds = intval($userAmount->remail_golds*100 + $data['money']*100)/100;
                    break;
                case 2://扣减
                    $userAmount->remail_golds = intval($userAmount->remail_golds*100 - $data['money']*100)/100;
                    break;
                case 3://冻结
                    $userAmount->remail_golds = intval($userAmount->remail_golds*100 - $data['money']*100)/100;
                    $userAmount->freezing_golds = intval($userAmount->freezing_golds*100 + $data['money']*100)/100;
                    break;
                case 4://解冻
                    $userAmount->freezing_golds = intval($userAmount->freezing_golds*100 - $data['money']*100)/100;
                    $userAmount->remail_golds = intval($userAmount->remail_golds*100 + $data['money']*100)/100;
                    break;
                case 5://扣除解冻
                    $userAmount->freezing_golds = intval($userAmount->freezing_golds*100 - $data['money']*100)/100;
                    break;
                default:
                    break;
            }
            $userAmount->update_time = time();
            if ($userAmount->save()) return $userAmount;
        }
        return false;
    }

    //转出账户资金
    public function popBank($data) {
        $userAmount = BuyUserAccount::find()
            ->where(['buy_id' => $data['pop_role_id']])
            ->one();
        if ($userAmount) {
            if (1 == $data['action_type']) {//押金
                $userAmount->remain_funds = intval($userAmount->remain_funds*100 - $data['deposit']*100)/100;
            }
            if (2 == $data['action_type']) {//花椒
                $userAmount->remail_golds = intval($userAmount->remail_golds*100 - $data['pepper']*100)/100;
            }
            if (3 == $data['action_type']) {//花椒押金
                $userAmount->remail_golds = intval($userAmount->remail_golds*100 - $data['pepper']*100)/100;
                $userAmount->remain_funds = intval($userAmount->remain_funds*100 - $data['deposit']*100)/100;
            }
            if ($userAmount->save()) {
                return $userAmount;
            }
        }
        return false;
    }

    public function pushBank($data) {
        $userAmount = BuyUserAccount::find()
            ->where(['buy_id' => $data['push_role_id']])
            ->one();
        if ($userAmount) {
            if (1 == $data['action_type']) {//押金
                $userAmount->remain_funds = intval($userAmount->remain_funds*100 + $data['deposit']*100)/100;
            }
            if (2 == $data['action_type']) {//花椒
                $userAmount->remail_golds = intval($userAmount->remail_golds*100 + $data['pepper']*100)/100;
            }
            if (3 == $data['action_type']) {//花椒押金
                $userAmount->remail_golds = intval($userAmount->remail_golds*100 + $data['pepper']*100)/100;
                $userAmount->remain_funds = intval($userAmount->remain_funds*100 + $data['deposit']*100)/100;
            }
            if ($userAmount->save()) {
                return $userAmount;
            }
        }
        return false;
    }

    public function pushBank_all($data) {
        $userAmount = BuyUserAccount::find()
            ->where(['buy_id' => $data['push_role_id']])
            ->one();
        if ($userAmount) {
            if (1 == $data['action_type']) {//押金
                $userAmount->remain_funds = intval($userAmount->remain_funds*100 + $data['deposit']*100)/100;
            }
            if (2 == $data['action_type']) {//花椒
                $userAmount->remail_golds = intval($userAmount->remail_golds*100 + $data['pepper']*100)/100;
            }
            if (3 == $data['action_type']) {//花椒押金
                $userAmount->remail_golds = intval($userAmount->remail_golds*100 + $data['pepper']*100)/100;
                $userAmount->remain_funds = intval($userAmount->remain_funds*100 + $data['deposit']*100)/100;
            }
            if ($userAmount->save()) {
                return $userAmount;
            }
        }
        return false;
    }
    public function addUserAccount($data)
    {
        return Yii::$app->db->createCommand()->insert('buyuser_account', [
            'buy_id' =>  $data['buy_id'],
            'remain_funds' => $data['remain_funds'],
            'remail_golds' => $data['remail_golds'],
            'freezing_funds' => $data['freezing_funds'],
            'freezing_golds' => $data['freezing_golds'],
            'income_golds' => $data['income_golds'],
            'create_time' => time()
        ])->execute();
    }

    public function modifyBuyUserRemailGolds($data)
    {
        $sql = 'UPDATE ' . BuyUserAccount::tableName() . ' SET remail_golds = :remail_golds,update_time = :update_time WHERE buy_id = :buy_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':remail_golds' => $data['remail_golds'],
                ':update_time' => time(),
                ':buy_id' => $data['buy_id'],
            ])->execute();
    }

    //修改收益花椒和花椒余额
    public function modifyIncomeUserAccount($data)
    {
        $sql = 'UPDATE ' . BuyUserAccount::tableName() . ' SET remail_golds = :remail_golds,income_golds = :income_golds,update_time = :update_time WHERE buy_id = :buy_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':remail_golds' => $data['remail_golds'],
                ':income_golds' => $data['income_golds'],
                ':update_time' => time(),
                ':buy_id' => $data['buy_id'],
            ])->execute();
    }

    //修改本金和佣金记录
    public function  modifyUserAccount($data)
    {
        $sql = 'UPDATE ' . BuyUserAccount::tableName() . ' SET remain_funds = :remain_funds,remail_golds = :remail_golds,update_time = :update_time WHERE buy_id = :buy_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':remain_funds' => $data['remain_funds'],
                ':remail_golds' => $data['remail_golds'],
                ':update_time' => time(),
                ':buy_id' => $data['buy_id'],
            ])->execute();
    }
    //冻结花椒
    public function freezingGoldsPepper($data)
    {
        $sql = 'UPDATE ' . BuyUserAccount::tableName() . ' SET remail_golds = :remail_golds,freezing_golds = :freezing_golds,update_time = :update_time WHERE buy_id = :buy_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':remail_golds' => $data['remail_golds'],
                ':freezing_golds' => $data['freezing_golds'],
                ':update_time' => time(),
                ':buy_id' => $data['buy_id'],
            ])->execute();
    }
    //扣除冻结
    public function deductFreezingPepper($data)
    {
        $sql = 'UPDATE ' . BuyUserAccount::tableName() . ' SET freezing_golds = :freezing_golds,update_time = :update_time WHERE buy_id = :buy_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':freezing_golds' => $data['freezing_golds'],
                ':update_time' => time(),
                ':buy_id' => $data['buy_id'],
            ])->execute();
    }

    public function modifyBuyUserAccount($data)
    {
        $sql = 'UPDATE ' . BuyUserAccount::tableName() . ' SET remain_funds = :remain_funds,remail_golds = :remail_golds,freezing_funds = :freezing_funds,freezing_golds = :freezing_golds,update_time = :update_time WHERE buy_id = :buy_id ';
        return Yii::$app->db->createCommand($sql)
            ->bindValues([
                ':remain_funds' => $data['remain_funds'],
                ':remail_golds' => $data['remail_golds'],
                ':freezing_funds' => $data['freezing_funds'],
                ':freezing_golds' => $data['freezing_golds'],
                ':update_time' => time(),
                ':buy_id' => $data['buy_id'],
            ])->execute();
    }

}